package org.railage.client.gui.tabs;

import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import org.apache.log4j.Logger;
import org.railage.client.Client;
import org.railage.client.gui.main.GUI;

import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.UIManager;

public class EditStationsTab extends AbstractTab {

	private static final long serialVersionUID = -3404641839514507539L;

	private static final Logger log = Logger.getLogger(EditStationsTab.class);

	private static final String DELETE_CONFIRM_TITLE = "Station delete confirmation";
	private JTable stationsTable;

	private String[] stations = null;
	private String[] columnNames = new String[] { "Station name" };
	private JTextField stationField;

	public EditStationsTab() {
		setLayout(null);

		JTextPane info = new JTextPane();
		info.setBackground(UIManager.getColor("Panel.background"));
		info.setText("To add new station insert its name and press \"Add\".\r\n\r\nTo delete existing station double-click it in the list.");
		info.setBounds(10, 11, 337, 69);
		add(info);

		JLabel lblStation = new JLabel("Station");
		lblStation.setBounds(10, 121, 46, 14);
		add(lblStation);

		stationField = new JTextField();
		stationField.setBounds(66, 118, 182, 20);
		add(stationField);
		stationField.setColumns(10);

		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addStation(stationField.getText());
			}
		});
		btnAdd.setBounds(258, 117, 89, 23);
		add(btnAdd);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(369, 0, 351, 390);
		add(scrollPane);

		stationsTable = new JTable();
		stationsTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int row = stationsTable.rowAtPoint(e.getPoint());
					String stationName = (String) stationsTable.getValueAt(row,
							0);
					deleteStation(stationName);
				}
			}
		});
		scrollPane.setViewportView(stationsTable);
	}

	public void init() {
		fillTable();
	}

	private void fillTable() {
		Object[][] data = new Object[][] {};
		try {
			stations = (String[]) Client.getInstance().callMethod(
					"getStationNames", null);
			data = new Object[stations.length][];
			int i = 0;
			for (String station : stations) {
				data[i] = new Object[] { station };
				i++;
			}
		} catch (IOException e) {
			log.error(GUI.DISCON_UNEXP_LOG_MSG, e);
			JOptionPane.showMessageDialog(this, GUI.DISCON_UNEXP_GUI_MSG);
		}
		stationsTable.setModel(new DefaultTableModel(data, columnNames) {
			private static final long serialVersionUID = 8563643006246465987L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		});
	}

	private void addStation(String stationName) {
		String result = null;
		try {
			result = (String) Client.getInstance().callMethod("addStation",
					stationName);
			if (!result.equals("Station added")) {
				JOptionPane.showMessageDialog(this, result);
			}
		} catch (IOException e) {
			log.error(GUI.DISCON_UNEXP_LOG_MSG, e);
			JOptionPane.showMessageDialog(this, GUI.DISCON_UNEXP_GUI_MSG);
		}
		fillTable();
	}

	private void deleteStation(String stationName) {
		int confirmed = JOptionPane.showConfirmDialog(this,
				"Are you sure you want to delete " + stationName + " station?",
				DELETE_CONFIRM_TITLE, JOptionPane.YES_NO_OPTION);
		if (confirmed == 0) {
			String result = null;
			try {
				result = (String) Client.getInstance().callMethod(
						"deleteStation", stationName);
				if (!result.equals("Station deleted")) {
					JOptionPane.showMessageDialog(this, result);
				}
			} catch (IOException e) {
				log.error(GUI.DISCON_UNEXP_LOG_MSG, e);
				JOptionPane.showMessageDialog(this, GUI.DISCON_UNEXP_GUI_MSG);
			}
			fillTable();
		}
	}
}
